
clear all
set more off
capture log close
est clear


global table1 		0 // 1: run, 0: not 
global table2 		0 // 1: run, 0: not 
global table3 		0 // 1: run, 0: not 
global table4   	0 // 1: run, 0: not 
global table5 		0 // 1: run, 0: not 
global table6 		0 // 1: run, 0: not 


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* sumstat_platform *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $table1 == 1 {

use "data/working/data_platform.dta", clear

*------------------------------------------------------------------------------*
* Variables *
*------------------------------------------------------------------------------*

gen aware = heard_of_crypto!="Cryptocurrencies? What are they?"

bys household_id: egen choice_tot = sum(choice)

gen choice_any = (choice_tot > 0) if !missing(choice_tot)

replace quantity_new = quantity_new/1000

gen first_time_early = (first_purcha!="" & first_time==0)
rename first_time first_time_late

replace incomenewdummy = 1-incomenewdummy 
replace agenewdummy = 1-agenewdummy 

*------------------------------------------------------------------------------*
* Table *
*------------------------------------------------------------------------------*

* part 1
eststo sharepost: estpost sum agenewdummy incomenewdummy countrynewdummy type_objective ///	
							aware choice_any if currency_id == "bitcoin", detail

esttab sharepost using "output/tables/table1.tex", ///
							cells("count(fmt(%12.0fc)) mean(fmt(%12.2fc)) sd(fmt(%12.2fc)) min(fmt(%12.2fc)) p50(fmt(%12.2fc)) max(fmt(%12.2fc))") ///
							refcat(agenewdummy "\emph{Demographics:}" aware "\emph{Cryptocurrency questions (general):}", nolabel) ///
							coeflabel(agenewdummy "\quad Age $\leq$ 30" incomenewdummy "\quad Income $\leq$ \\$100K" ///
								countrynewdummy "\quad Outside US" type_objective "\quad Accredited investor" ///
								aware "\quad Awareness" choice_any "\quad Holding (at least one crypto)") ///
							collabels("Observations" "Mean" "Std. Dev." "Minimum" "Median" "Maximum") ///
							noobs nomtitle eqlabels(none) ///
							prefoot("") postfoot("")  replace

* part 2						
eststo sharepost: estpost sum choice_tot quantity_new first_time_late if currency_id == "bitcoin" & choice_any>0, detail

esttab sharepost using "output/tables/table1.tex", ///
							cells("count(fmt(%12.0fc)) mean(fmt(%12.2fc)) sd(fmt(%12.2fc)) min(fmt(%12.2fc)) p50(fmt(%12.2fc)) max(fmt(%12.2fc))") ///
							coeflabel(choice_tot "\quad Holding (number of cryptos)" quantity_new "\quad Holding (\\$.000)" ///
								first_time_late "\quad Late buyers (2018)") ///
							noobs nomtitle eqlabels(none) nolines collabels("") ///
							prehead("") posthead("") prefoot("") postfoot("")  append

* part 3
eststo sharepost: estpost sum short_increase short_decrease long_decrease if currency_id == "bitcoin", detail
							
esttab sharepost using "output/tables/table1.tex", ///
							cells("count(fmt(%12.0fc)) mean(fmt(%12.2fc)) sd(fmt(%12.2fc)) min(fmt(%12.2fc)) p50(fmt(%12.2fc)) max(fmt(%12.2fc))") ///
							refcat(short_increase "\emph{Cryptocurrency questions (beliefs):}", nolabel) ///
							coeflabel(short_increase "\quad Increase" short_decrease "\quad Decrease" ///
								long_decrease  "\quad Never mainstream") ///
							noobs nomtitle eqlabels(none) nolines collabels("") ///
							prehead("") posthead("") prefoot("") postfoot("")  append

* part 4							
eststo sharepost: estpost sum currencies_potential, detail

esttab sharepost using "output/tables/table1.tex", ///
							cells("count(fmt(%12.0fc)) mean(fmt(%12.2fc)) sd(fmt(%12.2fc)) min(fmt(%12.2fc)) p50(fmt(%12.2fc)) max(fmt(%12.2fc))") ///
							coeflabel(currencies_potential "\quad High potential") ///
							noobs nomtitle eqlabels(none) collabels("") ///
							prehead("") posthead("")  append 
							
est clear


}


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* sumstat_compare *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $table2 == 1 {

*------------------------------------------------------------------------------*
* Sharepost *
*------------------------------------------------------------------------------*

use "data/working/data_platform.dta", clear

gen aware = heard_of_crypto!="Cryptocurrencies? What are they?"

bys household_id: egen choice_tot = sum(choice)

gen choice_any = (choice_tot > 0) if !missing(choice_tot)

keep if currency_id == "bitcoin"
keep if country == "North America"

replace agenewdummy = 1-agenewdummy 
replace incomenewdummy = 1-incomenewdummy 

eststo sharepost: estpost sum agenewdummy incomenewdummy aware choice_any short_increase short_decrease 

*------------------------------------------------------------------------------*
* SCPC *
*------------------------------------------------------------------------------*

use "data/working/working_scpc.dta", clear

keep if scpc_year == 2018

rename bitcoin_heardof 	aware
rename bitcoin_own  	choice_any
rename exp_inc_year   	short_increase
rename exp_dec_year   	short_decrease

drop agenewdummy 
gen agenewdummy = (age <= 30)

eststo scpc: estpost sum agenewdummy incomenewdummy aware choice_any short_increase short_decrease 

*------------------------------------------------------------------------------*
* ING *
*------------------------------------------------------------------------------*

use "data/working/working_ING.dta", clear

keep if Country == "USA"

rename heard 	aware
rename own 		choice_any
rename exp_inc 	short_increase
rename exp_dec 	short_decrease

drop agenewdummy
gen agenewdummy = (age <= 30)

eststo ing: estpost sum agenewdummy aware choice_any short_increase short_decrease 

*------------------------------------------------------------------------------*
* Table *
*------------------------------------------------------------------------------*
	
esttab 	sharepost scpc ing  ///
		using "output/tables/table2.tex", replace booktabs b(3) se(3) ///
							cells("count(fmt(%12.0fc)) mean(fmt(%12.2fc))") ///
							refcat(agenewdummy "\emph{Demographics:}" aware "\emph{Cryptocurrency questions (general):}" ///
										short_increase "\emph{Cryptocurrency questions (beliefs):}", nolabel) ///
							coeflabel(  agenewdummy "\quad Age $\leq$ 30" incomenewdummy "\quad Income $\leq$ \\$100K"  ///
										aware "\quad Awareness" choice_any "\quad Holding"  ///
										short_increase "\quad Increase" short_decrease "\quad Decrease") ///
							mgroups("\textsc{Trading Company}" "\textsc{SCPC}" "\textsc{ING}", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) /// 
							mtitle("Count" "Mean" "Count" "Mean" "Count" "Mean")  /// 
							noobs label eqlabels(none) nonumber
							
est clear	
							
}


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* belief_platform *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $table3 == 1 {

use "data/working/data_platform.dta", clear

*------------------------------------------------------------------------------*
* Variables *
*------------------------------------------------------------------------------*

rename first_time first_time_late
gen first_time_early = (first_purcha!="" & first_time_late==0)

replace incomenewdummy = 1-incomenewdummy 
replace agenewdummy = 1-agenewdummy 

gen long_increase = -long_decrease + 1

sort currency_id
encode currency_id, gen(currency_id_num)

gen top3 	= currency_id == "bitcoin" | currency_id == "ethereum" | currency_id == "ripple"

gen 	exp = 0 	if expectationsvalue=="Stay the same"
replace exp = 1 	if expectationsvalue=="Increase"
replace exp = -1 	if expectationsvalue=="Decrease"
label define explabel -1 "Decrease" 0 "Stay the same" 1 "Increase"
label values exp explabel

*------------------------------------------------------------------------------*
* Estimation *
*------------------------------------------------------------------------------*


local control 	incomenewdummy agenewdummy countrynewdummy type_objective 
local var 		1.first_time_early 1.first_time_late 

* short exp
oprob exp `var' `control' 1.wave if currency_id == "bitcoin" 
				estadd local fe_wave 		"Yes"
				estadd local fe_currency 	"No"
				estadd ysumm
est sto sharespost1

	
* short exp with interaction
oprob exp `var' `control' 1.wave 1.first_time_early#1.wave 1.first_time_late#1.wave if currency_id == "bitcoin"  
				estadd local fe_wave 		"Yes"
				estadd local fe_currency 	"No"
				estadd ysumm
est sto sharespost2

* long exp
probit long_increase `var'  `control' 1.wave if currency_id == "bitcoin"
				estadd local fe_wave 		"Yes"
				estadd local fe_currency 	"No"
				estadd ysumm
est sto sharespost3

* long exp with interaction
probit long_increase `var'  `control' 1.wave 1.first_time_early#1.wave 1.first_time_late#1.wave if currency_id == "bitcoin"
				estadd local fe_wave 		"Yes"
				estadd local fe_currency 	"No"
				estadd ysumm
est sto sharespost4

* potential
probit currencies_potential `var' `control' 1.wave i.currency_id_num, cl(household_id)
				estadd local fe_wave 		"Yes"
				estadd local fe_currency 	"Yes"
				estadd ysumm
est sto sharespost5

* potential with interaction
probit currencies_potential `var' `control' 1.wave i.currency_id_num 1.first_time_early#1.top3  1.first_time_late#1.top3, cl(household_id) 
				estadd local fe_wave 		"Yes"
				estadd local fe_currency 	"Yes"
				estadd ysumm
est sto sharespost6


*------------------------------------------------------------------------------*
* Table *
*------------------------------------------------------------------------------*
	
esttab 	sharespost1 sharespost3 sharespost5 sharespost6 ///
		using "output/tables/table3.tex", replace booktabs b(3) se(3) ///
			keep(incomenewdummy agenewdummy countrynewdummy type_objective *first_time_early* *first_time_late*) ///
			order(incomenewdummy agenewdummy countrynewdummy type_objective *first_time_early* *first_time_late*) ///
			coeflabel(incomenewdummy "\quad Income $\leq$ \\$100K" agenewdummy "\quad Age $\leq$ 30"  ///
						countrynewdummy "\quad Outside US" type_objective  "\quad Accredited investor" ///
						1.wave "\quad Boom (January 2018)" ///
						1.first_time_early "\quad Early Buyer" 1.first_time_late "\quad Late Buyer" ///
						1.first_time_early#1.wave 	"\quad Early Buyer $\times$ Boom" ///
						1.first_time_late#1.wave 	"\quad Late Buyer $\times$ Boom" ///
						1.first_time_early#1.top3 	"\quad Early Buyer $\times$ Top3" ///
						1.first_time_late#1.top3 	"\quad Late Buyer $\times$ Top3") ///
			star(* 0.10 ** 0.05 *** 0.01) nonotes stats(fe_wave fe_currency ymean ysd r2_p N, fmt(0 0 2 2 2 %9.0fc) ///
							labels( "Wave f.e." "Cryptocurrency f.e." "Mean Y" "SD Y" "Pseudo R2" "Observations" ) )  /// 
			mgroups("\textsc{\shortstack{Short-term\\Beliefs}}" "\textsc{\shortstack{Long-term\\Beliefs}}" "\textsc{High Potential}", pattern(1 1 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
			refcat(incomenewdummy "\emph{Demographics:}" 1.first_time_early "\emph{Other variables:}", nolabel) nomtitle eqlabel(none) //


est clear
						

}


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* beliefdemand_platform *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $table4 == 1 {

use "data/working/data_platform.dta", clear


*------------------------------------------------------------------------------*
* Variables *
*------------------------------------------------------------------------------*

replace incomenewdummy 	= 1-incomenewdummy
replace agenewdummy 	= 1-agenewdummy 

sort currency_id
encode currency_id, gen(currency_id_num)

bys household_id: egen choice_tot = sum(choice)
gen choice_any = (choice_tot > 0) if !missing(choice_tot)

gen log_quantity = log(quantity_new)

replace quantity_new = quantity_new/1000

bys household_id: egen currencies_potential_tot = sum(currencies_potential)

gen crypto_share = 100 * quantity_new / (income_new/1000 * 6.6)

gen not_casual =  typeobjective == "investor"

*------------------------------------------------------------------------------*
* Estimation *
*------------------------------------------------------------------------------*

keep if currency_id == "bitcoin"

local control incomenewdummy agenewdummy countrynewdummy type_objective 

foreach var in choice crypto_share {
	
	* short exp only
	reg `var' short_increase short_decrease
				estadd local fe_wave 		"Yes"
				estadd local add_controls	"Yes"
				estadd ysumm
	est sto `var'_1

	* + borrower controls
	reg `var'  short_increase short_decrease `control' i.wave  
				estadd local fe_wave 		"Yes"
				estadd local add_controls	"Yes"
				estadd ysumm
	est sto `var'_2	
	
	* + long term
	if "`var'" == "choice" {
		reg `var'  short_increase short_decrease long_decrease currencies_potential `control' i.wave  
					estadd local fe_wave 		"Yes"
					estadd local add_controls	"Yes"
					estadd ysumm
		est sto `var'_3	
	}
	if "`var'" == "crypto_share" {
		reg `var'  short_increase short_decrease long_decrease currencies_potential_tot `control' i.wave  
					estadd local fe_wave 		"Yes"
					estadd local add_controls	"Yes"
					estadd ysumm
		est sto `var'_3	
	}
	
	* + interactions
	reg `var'  short_increase 1.short_increase#1.not_casual short_decrease 1.short_decrease#1.not_casual long_decrease currencies_potential `control' i.wave  
				estadd local fe_wave 		"Yes"
				estadd local add_controls	"Yes"
				estadd ysumm
	est sto `var'_4	

}
*
rename crypto_share crypto_share_rename // just for saving purposes

foreach var in choice_tot quantity_new crypto_share_rename {

	* short exp only
	reg `var' short_increase short_decrease 					if choice_tot>0
				estadd local fe_wave 		"Yes"
				estadd local add_controls	"Yes"
				estadd ysumm
	est sto `var'_1

	* + borrower controls
	reg `var'  short_increase short_decrease `control' i.wave  if choice_tot>0
				estadd local fe_wave 		"Yes"
				estadd local add_controls	"Yes"
				estadd ysumm
	est sto `var'_2	
	
	* + long term
	reg `var'  short_increase short_decrease long_decrease currencies_potential_tot `control' i.wave  if choice_tot>0
				estadd local fe_wave 		"Yes"
				estadd local add_controls	"Yes"
				estadd ysumm
	est sto `var'_3	
	
	* + interactions
	reg `var'  short_increase 1.short_increase#1.not_casual short_decrease 1.short_decrease#1.not_casual long_decrease currencies_potential_tot `control' i.wave  if choice_tot>0 
				estadd local fe_wave 		"Yes"
				estadd local add_controls	"Yes"
				estadd ysumm
	est sto `var'_4	

	
}


*------------------------------------------------------------------------------*
* Table *
*------------------------------------------------------------------------------*

esttab 	choice_3 		crypto_share_3 	 /// 
		choice_tot_3  	quantity_new_3 	crypto_share_rename_3  /// 
		using "output/tables/table4.tex", replace booktabs b(3) se(3) ///
			keep(short_increase short_decrease  long_decrease currencies_potential currencies_potential_tot incomenewdummy agenewdummy countrynewdummy type_objective ) ///
			order(short_increase short_decrease long_decrease currencies_potential currencies_potential_tot incomenewdummy agenewdummy countrynewdummy type_objective ) ///
			coeflabel(	short_increase "\quad Price Increase" short_decrease "\quad Price Decrease"  /// 
						long_decrease 	"\quad Never mainstream"  currencies_potential "\quad High Potential (Dummy)"  /// 
						currencies_potential_tot "\quad High Potential (Number)"  ///
						incomenewdummy "\quad Income $\leq$ \\$100K" agenewdummy "\quad Age $\leq$ 30"  ///
						countrynewdummy "\quad Outside US" type_objective  "\quad Accredited investor" ) ///
			star(* 0.10 ** 0.05 *** 0.01) nonotes stats(fe_wave ymean ysd r2_a N, fmt(0 2 2 2 %9.0fc) ///
							labels( "Wave f.e." "Mean Y" "SD Y" "Adjusted R2" "Observations" ) )  ///
			mgroups("\textsc{Full Sample}" "\textsc{Investors with Positive Holdings}", pattern(1 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
			mtitle("\textsc{\shortstack{Invest in\\Bitcoin}}" "\textsc{\shortstack{Crypto\\share (\%)}}" ///
					"\textsc{\shortstack{Number of\\Crypto}}" "\textsc{\shortstack{Amount\\(\\$.000)}}" "\textsc{\shortstack{Crypto\\share (\%)}}") ///
			refcat(short_increase "\emph{Beliefs (short-term):}" long_decrease "\emph{Beliefs (long-term):}" incomenewdummy "\emph{Demographics:}", nolabel) //		
est clear



}


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* demand_main *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if ${table5} == 1 {


* column 1: only characteristics
est use "output/estimates/table5_col1"
	ereturn disp
	estadd local controls_macro "Yes"
	estadd local controls_demo	"Yes"
est sto main_1	

* column 2: + short-term beliefs
est use "output/estimates/table5_col2"
	ereturn disp
	estadd local controls_macro "Yes"
	estadd local controls_demo	"Yes"
est sto main_2	

* column 3: + never mainstream
est use "output/estimates/table5_col3"
	ereturn disp
	estadd local controls_macro "Yes"
	estadd local controls_demo	"Yes"
est sto main_3	

* column 4: + high potential 
est use "output/estimates/table5_col4"
	ereturn disp
	estadd local controls_macro "Yes"
	estadd local controls_demo	"Yes"
est sto main_4	


*------------------------------------------------------------------------------*
* Table *
*------------------------------------------------------------------------------*

esttab 	main_* ///
		using "output/tables/table5.tex", replace booktabs b(3) se(3) ///
			keep(logME pow beta momentum4 short_increase long_decrease currencies_potential) ///
			order(logME pow beta momentum4 short_increase long_decrease currencies_potential) ///
			coeflabel(	logME "\quad Market capitalization" pow "\quad Proof-of-Work" ///
						beta "\quad Beta" momentum4 "\quad Four-week momentum" ///
						short_increase "\quad Price Increase"  /// 
						long_decrease 	"\quad Never Mainstream"  currencies_potential "\quad High Potential"  ) ///
			star(* 0.10 ** 0.05 *** 0.01) nonotes stats(controls_macro controls_demo N, fmt(0 0 %9.0fc) ///
							labels( "Macro controls"  "Demographics controls" "Observations" ) )  ///
			refcat(logME "\emph{Characteristics:}" short_increase "\emph{Beliefs:}", nolabel) eqlabel(none) //		

* for Gio			
*esttab 	main_* ///
*		using "tables/demand_main.csv", replace 
est clear


}



*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* demand_heterogeneity *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if ${table6} == 1 {


* column 1: low income
est use "output/estimates/table6_col1.ster"
	ereturn disp
	estadd local controls_macro "Yes"
	estadd local controls_demo	"Yes"
est sto hete_1	

* column 2: high income
est use "output/estimates/table6_col2.ster"
	ereturn disp
	estadd local controls_macro "Yes"
	estadd local controls_demo	"Yes"
est sto hete_2	

* column 3: young
est use "output/estimates/table6_col3.ster"
	ereturn disp
	estadd local controls_macro "Yes"
	estadd local controls_demo	"Yes"
est sto hete_3	

* column 4: old
est use "output/estimates/table6_col4.ster"
	ereturn disp
	estadd local controls_macro "Yes"
	estadd local controls_demo	"Yes"
est sto hete_4	

* column 5: boom only
est use "output/estimates/table6_col5.ster"
	ereturn disp
	estadd local controls_macro "Yes"
	estadd local controls_demo	"Yes"
est sto hete_5	

* column 6: bust only
est use "output/estimates/table6_col6.ster"
	ereturn disp
	estadd local controls_macro "Yes"
	estadd local controls_demo	"Yes"
est sto hete_6	


*------------------------------------------------------------------------------*
* Table *
*------------------------------------------------------------------------------*

esttab 	hete_* ///
		using "output/tables/table6.tex", replace booktabs b(3) se(3) ///
			keep(logME pow beta momentum4 short_increase long_decrease currencies_potential) ///
			order(logME pow beta momentum4 short_increase long_decrease currencies_potential) ///
			coeflabel(	logME "\quad Market capitalization" pow "\quad Proof-of-Work" ///
						beta "\quad Beta" momentum4 "\quad Four-week momentum" ///
						short_increase "\quad Price Increase"  /// 
						long_decrease 	"\quad Never Mainstream"  currencies_potential "\quad High Potential"  ) ///
			star(* 0.10 ** 0.05 *** 0.01) nonotes stats(controls_macro controls_demo N, fmt(0 0 %9.0fc) ///
							labels( "Macro controls"  "Demographics controls" "Observations" ) )  ///
			refcat(logME "\emph{Characteristics:}" short_increase "\emph{Beliefs:}", nolabel) ///
			mgroups("\textsc{By income}" "\textsc{By age}" "\textsc{By period}", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
			mtitle("\textsc{$\leq$ \\$100K}" "\textsc{$>$ \\$100K}" "\textsc{$\leq$ 30}" "\textsc{$>$ 30}" "\textsc{Boom}" "\textsc{Bust}") eqlabel(none) ///

est clear


}


